package com.zzy.service;

import com.zzy.pojo.UserInfo;
import com.zzy.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public String login(String account, String password) {
        UserInfo userInfo = userRepository.findByUserNameAndUserPassword(account, password);
        if (userInfo == null) {
            return "用户名或密码错误";
        }
        return "登录成功";
    }

    public String register(UserInfo userInfo) {
        if (checkUserExist(userInfo.getUserName())) {
            return "用户已存在";
        }
        userInfo.setCreateDate(new Date());
        userRepository.save(userInfo);
        return "注册成功";
    }

    protected boolean checkUserExist(String userName) {
        UserInfo userInfo = userRepository.findByUserName(userName);
        if (userInfo == null) {
            return false;
        }
        return true;
    }
}
